package ServicosDAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import Model.Configuracao;

public class ConfiguracaoDAO {

	public boolean atualizarConfig(Configuracao config) {

		String sql = "update configuracao set dias_emprestimo=? ,qtd_emprestimo=? ,valor_multa=? where id='1'";

		Connection conexao = new ConexaoDAO().getConnection();

		int ok = 0;

		try {

			PreparedStatement stmt = conexao.prepareStatement(sql);

			stmt.setInt(1, config.getDiasEmprestimo());
			stmt.setInt(2, config.getQtdVolEmprestimo());
			stmt.setDouble(3, config.getValorMulta());

			ok = stmt.executeUpdate();

			stmt.close();
			conexao.close();

			if (ok == 1) {
				return true;
			}

		} catch (SQLException e) {

			e.printStackTrace();
			throw new RuntimeException();
		}
		return false;
	}




	public Configuracao selecionarConfig() {
		Connection conexao = new ConexaoDAO().getConnection();

		Configuracao configuracao = new Configuracao();

		try {
			ResultSet rs;

			String sql = "select * from configuracao";

			PreparedStatement stmt = conexao.prepareStatement(sql);

			rs = stmt.executeQuery();

			if (rs != null) {

				while (rs.next()) {

					configuracao
							.setDiasEmprestimo(rs.getInt("dias_emprestimo"));
					configuracao.setQtdVolEmprestimo(rs
							.getInt("qtd_emprestimo"));
					configuracao.setValorMulta(rs.getDouble("valor_multa"));

					return configuracao;
				}

				stmt.close();
				conexao.close();

				return configuracao;

			}

		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException();
		}

		return configuracao;
	}
}
